package com.wy.projectssj.controller;

import com.wy.projectssj.domain.PageBean;
import com.wy.projectssj.domain.User;
import com.wy.projectssj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
/**
 * @Author: wy
 * @Date:2019/8/28 19:51
 */
@Controller
@RequestMapping("/user")
public class UserController  {
      @Autowired
     private UserService userService;
     @Autowired
     private HttpServletRequest request;



        @RequestMapping("/login")
        public String userLogin(User user,Model model,String remember,HttpServletResponse response) throws UnsupportedEncodingException {
            User login = userService.login(user);

            if(login!=null) {
                String username = login.getUsername();
                username=URLEncoder.encode(username,"utf-8");
                Cookie c1=new Cookie("username",username);
                   Cookie c2=new Cookie("password",login.getPassword());
                   if("on".equals(remember)){
                       c1.setMaxAge(24*60*60);
                       c2.setMaxAge(24*60*60);
                       c1.setPath("/");
                       c2.setPath("/");
                   }else{
                       c1.setMaxAge(0);
                       c2.setMaxAge(0);
                       c1.setPath("/");
                       c2.setPath("/");
                   }

                response.addCookie(c1);
                   response.addCookie(c2);
               //将user对象存到session作用中
                  request.getSession().setAttribute("user",login);
                return "main";
            }else {
                  //存到request作用域中
                     request.getSession().setAttribute("errorLogin","");
                  return "redirect:/login.jsp";
            }
        }
             @RequestMapping("/loginOut")
          public String userLoginOut(){
             // request.getSession().removeAttribute("user");
                 request.getSession().invalidate();

                 return  "redirect:/login.jsp";
          }
          @RequestMapping("/addOne")
          public String addOne(){
             return "user-add";
          }
          @RequestMapping("/mainJsp")
          public String mainJsp(){
             return "main";
          }

       @RequestMapping("/findAll")
      public String findAll(@RequestParam(value="rows",defaultValue ="5" ) Integer rows,@RequestParam(value="currentPage",defaultValue = "1") Integer currentPage, Model model){
          PageBean<User> pb = userService.findAll(rows,currentPage);
           System.out.println(pb);
             model.addAttribute("pb",pb);

          return "user-list";
      }
          @RequestMapping("/save")
          public String save(User user){
              int result = userService.save(user);
              System.out.println(result);
              return "redirect:/user/findAll";

          }
            @RequestMapping("/deleteUser")
           public String delete(String[] ids){
                int result = userService.deleteUser(ids);

                  return "redirect:/user/findAll";
            }

             @RequestMapping("/findById")
            public String fingById(Model model,String id){

                 User user = userService.findById(id);
                 System.out.println(user);
                 model.addAttribute("user",user);
                 return "user-show";
             }
}
